package com.example.myys.data

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.example.myys.data.dao.ChapterDao
import com.example.myys.data.dao.DataBeanDao
import com.example.myys.data.dao.ReadRecordDao
import com.example.myys.data.dao.SettingLinkDao
import com.example.myys.data.entity.ChapterBean
import com.example.myys.data.entity.DataBean
import com.example.myys.data.entity.ReadRecordBean
import com.example.myys.data.entity.SettingLink

//数据库定义
@Database(entities = [
    SettingLink::class,
    DataBean::class,
    ChapterBean::class,
    ReadRecordBean::class], version = 1/*, autoMigrations = [AutoMigration(from = 1, to = 2)]*/, exportSchema = true)
abstract class InventoryDatabase : RoomDatabase() {
    abstract fun settingDao():SettingLinkDao
    abstract fun dataBeanDao(): DataBeanDao
    abstract fun chapterDao(): ChapterDao
    abstract fun readRecordDao(): ReadRecordDao
    companion object {
        @Volatile
        private var INSTANCE: InventoryDatabase? = null

        fun getInstance(context: Context): InventoryDatabase {
            synchronized(this) {
                var instance = INSTANCE

                if (instance == null) {
                    instance = Room.databaseBuilder(
                        context.applicationContext,
                        InventoryDatabase::class.java,
                        "myys_databases"
                    )
                        .allowMainThreadQueries() //容许主线程运行数据库
                        .fallbackToDestructiveMigration()
                        .build()
                    INSTANCE = instance
                }
                return instance
            }
        }
    }

}